using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Routing;

namespace XjjXmm.Infrastructure.Webs.Route
{
	public class RouteConvention : IApplicationModelConvention
	{
		private readonly AttributeRouteModel _routePrefix;
		/// <summary>
		/// 构造方法
		/// </summary>
		/// <param name="routeTemplateProvider"></param>
		public RouteConvention(IRouteTemplateProvider routeTemplateProvider)
		{
			_routePrefix = new AttributeRouteModel(routeTemplateProvider);
		}
 
		/// <summary>
		/// 实现接口的Apply方法
		/// </summary>
		/// <param name="application"></param>
		public void Apply(ApplicationModel application)
		{
			//遍历所有的 Controller
			foreach (var controller in application.Controllers)
			{
				// 已经标记了 RouteAttribute 的 Controller
				var matchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList();
				if (matchedSelectors.Any())
				{
					foreach (var selectorModel in matchedSelectors)
					{
						// 在当前路由上 再 添加一个路由前缀
						selectorModel.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_routePrefix, selectorModel.AttributeRouteModel);
					}
				}
 
				// 没有标记 RouteAttribute 的 Controller
				var unmatchedSelectors = controller.Selectors.Where(x => x.AttributeRouteModel == null).ToList();
				if (unmatchedSelectors.Any())
				{
					foreach (var selectorModel in unmatchedSelectors)
					{
						// 添加一个路由前缀
						selectorModel.AttributeRouteModel = _routePrefix;                       
					}
				}
			}
		}
	}
	
	
}
